home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / PRIM.C < prev    next >
C/C++ Source or Header  |  1989-09-20  |  8KB  |  256 lines

  1. /* -*-C-*-
  2.  
  3. $Header: prim.c,v 9.33 89/09/20 23:10:35 GMT cph Exp $
  4.  
  5. Copyright (c) 1988, 1989 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. /* The leftovers ... primitives that don't seem to belong elsewhere. */
  36.  
  37. #include "scheme.h"
  38. #include "prims.h"
  39.  
  40. /* Low level object manipulation */
  41.  
  42. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-TYPE", Prim_prim_obj_type, 1, 1,
  43.   "Return the type code of OBJECT as an unsigned integer.")
  44. {
  45.   PRIMITIVE_HEADER (1);
  46.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (OBJECT_TYPE (ARG_REF (1))));
  47. }
  48.  
  49. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-GC-TYPE", Prim_prim_obj_gc_type, 1, 1,
  50.   "Return an unsigned integer indicating the GC type of the object.")
  51. {
  52.   PRIMITIVE_HEADER (1);
  53.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (GC_Type_Map [OBJECT_TYPE (ARG_REF (1))]));
  54. }
  55.  
  56. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-TYPE?", Prim_prim_obj_type_p, 2, 2,
  57.   "Return #T if TYPE-CODE is OBJECT's type code, else #F.")
  58. {
  59.   PRIMITIVE_HEADER (2);
  60.   PRIMITIVE_RETURN
  61.     (BOOLEAN_TO_OBJECT
  62.      ((OBJECT_TYPE (ARG_REF (2))) ==
  63.       (arg_index_integer (1, (MAX_TYPE_CODE + 1)))));
  64. }
  65.  
  66. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-DATUM", Prim_prim_obj_datum, 1, 1,
  67.   "Return the datum part of OBJECT as an unsigned integer.")
  68. {
  69.   PRIMITIVE_HEADER (1);
  70.   PRIMITIVE_RETURN (long_to_integer (OBJECT_DATUM (ARG_REF (1))));
  71. }
  72.  
  73. DEFINE_PRIMITIVE ("MAKE-NON-POINTER-OBJECT", Prim_make_non_pointer_object, 1, 1,
  74.   "Convert the unsigned integer NUMBER into a fixnum.\n\
  75. Assert: (= (OBJECT-DATUM (MAKE-NON-POINTER-OBJECT X)) X).")
  76. {
  77.   PRIMITIVE_HEADER (1);
  78.   PRIMITIVE_RETURN
  79.     (LONG_TO_UNSIGNED_FIXNUM (arg_index_integer (1, (1 << DATUM_LENGTH))));
  80. }
  81.  
  82. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-SET-TYPE", Prim_prim_obj_set_type, 2, 2,
  83.   "Return a new object made from TYPE-CODE and the datum part of OBJECT.")
  84. {
  85.   PRIMITIVE_HEADER (2);
  86.   PRIMITIVE_RETURN
  87.     (OBJECT_NEW_TYPE
  88.      ((arg_index_integer (1, (MAX_TYPE_CODE + 1))), (ARG_REF (2))));
  89. }
  90.  
  91. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-EQ?", Prim_prim_obj_eq_p, 2, 2, 0)
  92. {
  93.   PRIMITIVE_HEADER (2);
  94.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT ((ARG_REF (1)) == (ARG_REF (2))));
  95. }
  96.  
  97. /* Low level memory references.
  98.  
  99.    Many primitives can be built out of these, and eventually should be.
  100.    These are extremely unsafe, since there is no consistency checking.
  101.    In particular, they are not gc-safe: You can screw yourself royally
  102.    by using them.  */
  103.  
  104. /* (PRIMITIVE-OBJECT-REF OBJECT INDEX)
  105.    Fetches the index'ed slot in object.
  106.    Performs no type checking on object.  */
  107.  
  108. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-REF", Prim_prim_obj_ref, 2, 2, 0)
  109. {
  110.   PRIMITIVE_HEADER (2);
  111.   PRIMITIVE_RETURN (MEMORY_REF ((ARG_REF (1)), (arg_nonnegative_integer (2))));
  112. }
  113.  
  114. /* (PRIMITIVE-OBJECT-SET! OBJECT INDEX VALUE)
  115.    Stores value in the index'ed slot in object.
  116.    Performs no type checking on object.  */
  117.  
  118. DEFINE_PRIMITIVE ("PRIMITIVE-OBJECT-SET!", Prim_prim_obj_set, 3, 3, 0)
  119. {
  120.   PRIMITIVE_HEADER (3);
  121.   MEMORY_SET ((ARG_REF (1)), (arg_nonnegative_integer (2)), (ARG_REF (3)));
  122.   PRIMITIVE_RETURN (UNSPECIFIC);
  123. }
  124.  
  125. /* Safe versions of the object manipulators.
  126.    These touch their arguments, and provide GC safety tests.  */
  127.  
  128. DEFINE_PRIMITIVE ("OBJECT-TYPE", Prim_object_type, 1, 1, 0)
  129. {
  130.   fast SCHEME_OBJECT object;
  131.   PRIMITIVE_HEADER (1);
  132.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  133.   PRIMITIVE_RETURN (LONG_TO_UNSIGNED_FIXNUM (OBJECT_TYPE (object)));
  134. }
  135.  
  136. DEFINE_PRIMITIVE ("OBJECT-GC-TYPE", Prim_object_gc_type, 1, 1, 0)
  137. {
  138.   fast SCHEME_OBJECT object;
  139.   PRIMITIVE_HEADER (1);
  140.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  141.   PRIMITIVE_RETURN (LONG_TO_FIXNUM (GC_Type (object)));
  142. }
  143.  
  144. DEFINE_PRIMITIVE ("OBJECT-TYPE?", Prim_object_type_p, 2, 2, 0)
  145. {
  146.   fast SCHEME_OBJECT object;
  147.   PRIMITIVE_HEADER (2);
  148.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object);
  149.   PRIMITIVE_RETURN
  150.     (BOOLEAN_TO_OBJECT
  151.      ((OBJECT_TYPE (object)) ==
  152.       (arg_index_integer (1, (MAX_TYPE_CODE + 1)))));
  153. }
  154.  
  155. DEFINE_PRIMITIVE ("OBJECT-DATUM", Prim_object_datum, 1, 1, 0)
  156. {
  157.   fast SCHEME_OBJECT object;
  158.   PRIMITIVE_HEADER (1);
  159.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  160.   PRIMITIVE_RETURN (long_to_integer (OBJECT_DATUM (object)));
  161. }
  162.  
  163. DEFINE_PRIMITIVE ("OBJECT-SET-TYPE", Prim_object_set_type, 2, 2, 0)
  164. {
  165.   fast long type_code;
  166.   fast SCHEME_OBJECT object;
  167.   PRIMITIVE_HEADER (2);
  168.   type_code = (arg_index_integer (1, (MAX_TYPE_CODE + 1)));
  169.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object);
  170.   {
  171.     fast long gc_type_code;
  172.  
  173.     gc_type_code = (GC_Type_Map [type_code]);
  174.     if ((gc_type_code == GC_Undefined) ||
  175.     (! ((gc_type_code == GC_Non_Pointer) ||
  176.         (gc_type_code == (GC_Type (object))))))
  177.       error_bad_range_arg (1);
  178.   }
  179.   PRIMITIVE_RETURN (OBJECT_NEW_TYPE (type_code, object));
  180. }
  181.  
  182. /* (EQ? OBJECT-1 OBJECT-2)
  183.    Returns #T if the two objects have the same type code and datum.
  184.    Returns #F otherwise.
  185.    Touches both arguments.  */
  186.  
  187. DEFINE_PRIMITIVE ("EQ?", Prim_eq, 2, 2, 0)
  188. {
  189.   fast SCHEME_OBJECT object_1;
  190.   fast SCHEME_OBJECT object_2;
  191.   PRIMITIVE_HEADER (2);
  192.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object_1);
  193.   TOUCH_IN_PRIMITIVE ((ARG_REF (2)), object_2);
  194.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (object_1 == object_2));
  195. }
  196.  
  197. /* (NOT OBJECT)
  198.    Returns #T if OBJECT is #F.  Otherwise returns #F.  This is
  199.    the primitive known as NOT, NULL?, and FALSE? in Scheme.
  200.    Touches the argument.  */
  201.  
  202. DEFINE_PRIMITIVE ("NOT", Prim_not, 1, 1, 0)
  203. {
  204.   fast SCHEME_OBJECT object;
  205.   PRIMITIVE_HEADER (1);
  206.   TOUCH_IN_PRIMITIVE ((ARG_REF (1)), object);
  207.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (object == SHARP_F));
  208. }
  209.  
  210. /* Cells */
  211.  
  212. /* (MAKE-CELL CONTENTS)
  213.    Creates a cell with contents CONTENTS. */
  214.  
  215. DEFINE_PRIMITIVE ("MAKE-CELL", Prim_make_cell, 1, 1, 0)
  216. {
  217.   PRIMITIVE_HEADER (1);
  218.   Primitive_GC_If_Needed (1);
  219.   (*Free++) = (ARG_REF (1));
  220.   PRIMITIVE_RETURN (MAKE_POINTER_OBJECT (TC_CELL, (Free - 1)));
  221. }
  222.  
  223. /* (CELL? OBJECT)
  224.    Returns #T if OBJECT is a cell, else #F.  */
  225.  
  226. DEFINE_PRIMITIVE ("CELL?", Prim_cell_p, 1, 1, 0)
  227. {
  228.   PRIMITIVE_HEADER (1);
  229.   PRIMITIVE_RETURN (BOOLEAN_TO_OBJECT (CELL_P (ARG_REF (1))));
  230. }
  231.  
  232. /* (CELL-CONTENTS CELL)
  233.    Returns the contents of the cell CELL.  */
  234.  
  235. DEFINE_PRIMITIVE ("CELL-CONTENTS", Prim_cell_contents, 1, 1, 0)
  236. {
  237.   PRIMITIVE_HEADER (1);
  238.   PRIMITIVE_RETURN (MEMORY_REF ((CELL_ARG (1)), CELL_CONTENTS));
  239. }
  240.  
  241. /* (SET-CELL-CONTENTS! CELL OBJECT)
  242.    Stores OBJECT as contents of CELL.
  243.    Returns the previous contents of CELL. */
  244.  
  245. DEFINE_PRIMITIVE ("SET-CELL-CONTENTS!", Prim_set_cell_contents, 2, 2, 0)
  246. {
  247.   fast SCHEME_OBJECT cell;
  248.   fast SCHEME_OBJECT object;
  249.   PRIMITIVE_HEADER (2);
  250.   cell = (CELL_ARG (1));
  251.   object = (ARG_REF (2));
  252.   SIDE_EFFECT_IMPURIFY (cell, object);
  253.   MEMORY_SET (cell, CELL_CONTENTS, object);
  254.   PRIMITIVE_RETURN (UNSPECIFIC);
  255. }
  256.